/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAOOracle;

import DAO.DAOMedico;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.LinkedList;
import modelo.Domicilio;
import modelo.Medico;

/**
 *
 * @author Nai
 */
public class MedicoDAOOracle implements DAOMedico {

    @Override
   public boolean insert(Medico medi) {
       Connection con = OracleDAOFactory.createConnection();
        boolean res = false;
        try {
            PreparedStatement st = con.prepareStatement("insert into MEDICOS " +
                    "(APELLIDO,NOMBRE,DOMICILIO_ID,TELEFONO,EMAIL,NUMERO_MATRICULA,USUARIO,CONTRASENA ) " +
                    "values (?,?,?,?,?,?,?,?)");
            st.setString(1, medi.getApellido());
            st.setString(2, medi.getNombre());
            st.setInt(3, medi.getDomicilio().getIdDomicilio());
            st.setInt(4, medi.getTelefono());
            st.setString(5, medi.getEmail());
            st.setString(6, medi.getNumeroMatricula());
            st.setString(7, medi.getUsuario());
            st.setString(8, medi.getContrasena());
            res = st.execute();
            con.close();
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
        return res;
        
    }

    @Override
    public boolean delete(Medico medi) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean update(Medico medi) {
        
        Connection con = OracleDAOFactory.createConnection();
        boolean res = false;
        try {
            PreparedStatement st = con.prepareStatement("update MEDICOS set " +
                    "ID_MEDICO=?,APELLIDO=?,NOMBRE=?,DOMICILIO_ID=?,TELEFONO=?,EMAIL=?,NUMERO_MATRICULA=?,USUARIO=?,CONTRASENA=?" +
                    "where ID_MEDICO=?");
             st.setInt(1, medi.getIdMedico());
            st.setString(2, medi.getApellido());
            st.setString(3, medi.getNombre());
            st.setInt(4, medi.getDomicilio().getIdDomicilio());
            st.setInt(5, medi.getTelefono());
            st.setString(6, medi.getEmail());
            st.setString(7, medi.getNumeroMatricula());
            st.setString(8, medi.getUsuario());
            st.setString(9, medi.getContrasena());
            
            st.setInt(10, medi.getIdMedico());
            
            res = st.execute();
            con.close();
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
        return res;
    }


    @Override
    public Medico find(Medico medi) {
        Connection con = OracleDAOFactory.createConnection();
        Medico med = null;

        try {//
            ResultSet rs = con.createStatement().executeQuery("select * from medicos where usuario='"+medi.getUsuario()+"'" );
            if (rs.next()) {//ID_MEDICO APELLIDO NOMBRE DOMICILIO_ID TELEFONO EMAIL NUMERO_MATRICULA USUARIO CONTRASENA

                med = new Medico();
                med.setIdMedico(rs.getInt(1));
                med.setApellido(rs.getString(2));
                med.setNombre(rs.getString(3));
                med.setDomicilio(new Domicilio (rs.getInt(4)));
                med.setTelefono(rs.getInt(5));
                med.setEmail(rs.getString(6));
                med.setNumeroMatricula(rs.getString(7));
                med.setUsuario(rs.getString(8));
                med.setContrasena(rs.getString(9));
                
            }
            con.close();
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
        return med;
    }

    @Override
    public Collection<Medico> list() {
        Connection con = OracleDAOFactory.createConnection();
        LinkedList<Medico> res = new LinkedList();

        try {//
            ResultSet rs = con.createStatement().executeQuery("select * from medicos order by APELLIDO, NOMBRE");
            while (rs.next()) {//ID_MEDICO APELLIDO NOMBRE DOMICILIO_ID TELEFONO EMAIL NUMERO_MATRICULA USUARIO CONTRASENA
                Medico med = new Medico();
                med.setIdMedico(rs.getInt(1));
                med.setApellido(rs.getString(2));
                med.setNombre(rs.getString(3));
                med.setDomicilio(new Domicilio (rs.getInt(4)));
                med.setTelefono(rs.getInt(5));
                med.setEmail(rs.getString(6));
                med.setNumeroMatricula(rs.getString(7));
                med.setUsuario(rs.getString(8));
                med.setContrasena(rs.getString(9));
                res.add(med);
            }
            con.close();
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
        return res;
    }
    
}
